#include "HerzeleidPrecompiled.hxx"
#include "HerzeleidCoreDispatcher.hxx"

using namespace HerzeleidUICoreNamespaceImpl;
#define CreateAsyncAction( Task, Token ) Concurrency::create_task( Task, Token )

CoreWindowMessage::CoreWindowMessage( ) : 
	Handle( 0 ), Message( 0ui32), WParam( 0ui32 ), LParam( 0l )
{
}

CoreWindowMessage::CoreWindowMessage( _In_ WindowHandleConst Handle, _In_ UInt32Const Message, _In_ WParamConst wParam, _In_ LParamConst lParam ) : 
	Handle(Handle), Message(Message), WParam(wParam), LParam(lParam) // Assing all the fields 
{
}

CoreDispatcher::CoreDispatcher( CoreWindowPtrConst Host ) : 
	//m_eClosingOption(CoreDispatcherClosingOption::DiscardAllIfAny), // Obviously, by default the dispatcher will close as fast as possible
	m_eClosingOption(CoreDispatcherClosingOption::ProcessAllIfAny), // By default the dispatcher will process all the captured events
	m_HostReferencePtr(Host), // Assing the reference to the creator window 
	m_bIgnoreProcessRequests(false), // Only after ProcessUntilQuit option is passed
	m_bPointerExitedClientRect(true), // After the first pointer updated event and after each pointer leaved event when hovering
	m_BoundingRectBuffer(CoreDispatcher::Rect::Empty()) // Updated on sizing event
{
	RestartAll();
}

void CoreDispatcher::RestartAll()
{
	// Recreate the cancellation token source for message capturing. 
	m_CancellationTokenSrcForCapturing = AsyncCancellationTokenSource(); 
	// Recreate the cancellation token source for message dispatching. 
	m_CancellationTokenSrcForDispatching = AsyncCancellationTokenSource(); 

	// Reinitialize message capturing task chain. 
	m_MsgCapturing = CreateAsyncAction( 
		[]( Void ) -> Bool { return( false ); }, 
		m_CancellationTokenSrcForCapturing.get_token() 
		); 
	// Reinitialize message dispatching task chain. 
	m_MsgDispatching = CreateAsyncAction( 
		[]( Void ) -> Bool { return( false ); }, 
		m_CancellationTokenSrcForDispatching.get_token() 
		); 

	// Clean up all the unprocessed window messages.
	m_Messages.clear();  
}